package ua.org.cityid.client.mvp;

import java.util.ArrayList;
import java.util.List;

import ua.org.cityid.client.controls.NavBar;
import ua.org.cityid.client.controls.RegionAnchor;
import ua.org.cityid.shared.data.City;
import ua.org.cityid.shared.data.Regions;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class MainView extends Composite implements MainPresenter.Display{

	private static mainPageUiBinder uiBinder = GWT
			.create(mainPageUiBinder.class);

	interface mainPageUiBinder extends UiBinder<Widget, MainView> {
	}
	
	@UiField
	Grid regionsList;
	@UiField
	FlexTable detailsHeader;
	@UiField
	FlexTable detailsTable;
	
	NavBar navBar = new NavBar();
	
	private List<RegionAnchor> regionAnchors = new ArrayList<RegionAnchor>();
	
	@Inject
	public MainView() {
		initWidget(uiBinder.createAndBindUi(this));
		initTable();
		setRegions();
	}
	
	private void initTable() {
	    final String CITY_ID_COLUMN_WIDTH 			= "40px";
		final String CITY_NAME_COLUMN_WIDTH 		= "150px";
	    final String CITY_PHONE_CODE_COLUMN_WIDTH 	= "100px";
//	    final String CITY_ZIP_CODES_COLUMN_WIDTH 	= "60px";
	    final String NAVBAR_COLUMN_WIDTH			= "300px";
	    
		// Initialize the header.
	    detailsHeader.getColumnFormatter().setWidth(0, CITY_ID_COLUMN_WIDTH);
	    detailsHeader.getColumnFormatter().setWidth(1, CITY_NAME_COLUMN_WIDTH);
	    detailsHeader.getColumnFormatter().setWidth(2, CITY_PHONE_CODE_COLUMN_WIDTH);
	    detailsHeader.getColumnFormatter().setWidth(4, NAVBAR_COLUMN_WIDTH);

	    detailsHeader.setText(0, 0, "id");
	    detailsHeader.setText(0, 1, "Город");
	    detailsHeader.setText(0, 2, "Телефонный код");
	    detailsHeader.setText(0, 3, "Почтовые коды");
	    detailsHeader.setWidget(0, 4, navBar);
	    detailsHeader.getCellFormatter().setHorizontalAlignment(0, 4, HasHorizontalAlignment.ALIGN_RIGHT);

	    // Initialize the table.
	    detailsTable.getColumnFormatter().setWidth(0, CITY_ID_COLUMN_WIDTH);
	    detailsTable.getColumnFormatter().setWidth(1, CITY_NAME_COLUMN_WIDTH);
	    detailsTable.getColumnFormatter().setWidth(2, CITY_PHONE_CODE_COLUMN_WIDTH);
	    detailsTable.getColumnFormatter().setWidth(3, "*");
	}
	
	private void setRegions(){
		regionsList.resize(5, 5);
		for (int index = 0; index < Regions.list.length; index++){
			RegionAnchor a = new RegionAnchor(index, Regions.list[index]);
			regionsList.setWidget(index / 5,  index % 5 , a);
			regionAnchors.add(a);
		}
	}

	@Override
	public List<RegionAnchor> getRegionAnchors() {
		return regionAnchors;
	}

	@Override
	public HasClickHandlers getNavBarNext() {
		return navBar.getButtonNext();
	}
	
	@Override
	public HasClickHandlers getNavBarPrev() {
		return navBar.getButtonPrev();
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void startProcessing() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void stopProcessing() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void fillCitiesTable(List<City> cities) {
		int row = 0;
		detailsTable.getRowCount();
		for (City c : cities) {
			detailsTable.setText(row, 0, c.getId().toString());
			detailsTable.setText(row, 1, c.getName());
			detailsTable.setText(row, 2, Integer.toString(c.getPhoneCode()));
			row++;
		}
		while (row < detailsTable.getRowCount()) {
			detailsTable.setText(row,0, "");
			detailsTable.setText(row,1, "");
			detailsTable.setText(row,2, "");
			row++;
		}
		
	}

	@Override
	public void updateNavBar(int itemsTotal, int startIndex) {
		navBar.update(startIndex, itemsTotal, MainPresenter.MAX_VISIBLE_REGIONS);
	}
}
